package com.song.sshTemplate.util.email;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;


public class SendMail {

	String sender;
	String password;
	String smtpServer ;
	String recipient;
	String subject;
	String fileAttachment ;
	String content ;

	public SendMail(String sender, String password, String smtpServer,
			String recipient, String subject, String fileAttachment,
			String content) {
		// 初始化信息
		this.sender = sender;
		this.password = password;
		this.smtpServer = smtpServer;
		this.recipient = recipient;
		this.subject = subject;
		this.fileAttachment = fileAttachment;
		this.content = content;
	}

	public boolean sendMail() {
		boolean flag = false;
		// 配置服务器属性
		Properties proper = new Properties();
		proper.put("mail.smtp.host", smtpServer); // smtp服务器
		proper.put("mail.smtp.auth", "true"); // 是否smtp认证
		proper.put("mail.smtp.port", "25"); // 设置smtp端口
		proper.put("mail.transport.protocol", "smtp"); // 发邮件协议
		proper.put("mail.store.protocol", "pop3"); // 收邮件协议
		proper.setProperty("mail.pop3.disabletop", "true"); 
		// 配置邮件接收地址
		InternetAddress[] receiveAddress = new InternetAddress[1];
		try {
			receiveAddress[0] = new InternetAddress(recipient);
		} catch (AddressException e) {
			e.printStackTrace();
		}
		// smtp认证，获取Session
		SmtpAuth sa = new SmtpAuth();
		sa.setUserinfo(sender, password);
		Session session = Session.getInstance(proper, sa);
		session.setPasswordAuthentication(new URLName(smtpServer), sa.getPasswordAuthentication());
		// 构建邮件体
		MimeMessage sendMess = new MimeMessage(session);
		MimeBodyPart mbp = new MimeBodyPart();
		MimeMultipart mmp = new MimeMultipart();
		try {
			// 邮件文本内容
			mbp.setContent(content, "text/plain; charset=GBK");
			mmp.addBodyPart(mbp);
			// 附件处理
			if (fileAttachment != null && !"".equals(fileAttachment)) {
				DataSource source = new FileDataSource(fileAttachment);
				String name = source.getName();
				mbp = new MimeBodyPart();
				mbp.setDataHandler(new DataHandler(source));
				// 将附件名称转码，防止中文乱码问题
//				sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
//				mbp.setFileName("=?GBK?B?" + enc.encode(name.getBytes()) + "?=");
//				mbp.setFileName("=?GBK?B?" + name + "?=");
				mbp.setFileName(MimeUtility.encodeText(name));
				mmp.addBodyPart(mbp);
			}
			// 邮件整体
			sendMess.setSubject(subject);
			sendMess.setContent(mmp);
			// 发送邮件
			sendMess.setFrom(new InternetAddress(sender));
			sendMess.setRecipients(Message.RecipientType.TO, receiveAddress);
			Transport.send(sendMess);
			flag = true;
			System.out.println("发送成功");
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return flag;
	}
}